package com.hongdee.crm.tdc.mapper;

import com.hongdee.crm.tdc.pojo.*;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author zhangkai
 * @desc 一句话描述
 **/
@Mapper
public interface MarketQuestionOrderMapper {

    /************ 查看是否存在有效的调研问券 *****************/
    /***
     * 查询系统配置的调研问券的编码
     * @param companyId
     * @return
     * @throws Exception
     */
    @SelectProvider(type = MarketQuestionOrderSQL.class, method = "queryParamSQL")
    @Results({
            @Result(column = "value", property = "code")
    })
    MarketNairePoJo queryParam(@Param("companyId") String companyId) throws Exception;


    /***
     * 查询是否存在有效的调研问券
     * @param companyId
     * @return
     * @throws Exception
     */
    @SelectProvider(type = MarketQuestionOrderSQL.class, method = "queryEnabelNarieSQL")
    @Results({
            @Result(column = "code", property = "code"),
            @Result(column = "status", property = "status"),
    })
    MarketNairePoJo queryQusetionOrder(@Param("companyId") String companyId, @Param("narieCode") String narieCode) throws Exception;


    /***
     * 查询是否存在 已匹配工单的调研问券
     * @param companyId
     * @return
     * @throws Exception
     */
    @SelectProvider(type = MarketQuestionOrderSQL.class, method = "queryOrderNumSQL")
    @Results({
            @Result(column = "score", property = "totalScore"),
    })
    MarketNairePoJo queryOrderNum(@Param("companyId") String companyId, @Param("orderNum") String orderNum) throws Exception;

    /****
     *  绑定调研问券
     * @param orderNum
     * @param code
     * @param companyId
     */
    @InsertProvider(type = MarketQuestionOrderSQL.class, method = "bindOrderInfoSQL")
    void bindOrderNumAndNarieNo(@Param("orderNum") String orderNum, @Param("narieCode") String code, @Param("companyId") String companyId);


    @SelectProvider(type = MarketQuestionOrderSQL.class, method = "queryItemSQL")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "label", property = "label"),
            @Result(column = "label_type", property = "labelType"),
            @Result(column = "value_type", property = "valueType"),
            @Result(column = "p_id", property = "pId"),
            @Result(column = "assgin_score", property = "assginScore"),
            @Result(column = "library_id", property = "libraryId"),
            @Result(column = "narie_code", property = "narieCode"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "score", property = "finalScore"),
            @Result(column = "order_num", property = "orderNum"),
    })
    List<MarketTdcQuestionScorePojo> queryQuestionItem(@Param("pId") String pId, @Param("orderNum") String orderNum, @Param("companyId") String companyId);

    /***
     * 用户回答问题-获取得分
     * @param id
     * @param score
     */
    @UpdateProvider(type = MarketQuestionOrderSQL.class, method = "updateOrderItemScoreSQL")
    void updateOrderScore(@Param("id") String id, @Param("score") Integer score);

    /****
     * 根据当前记录ID，计算上一级分数
     * @param id
     */
    @SelectProvider(type = MarketQuestionOrderSQL.class,method = "countParentItemScoreSQL")
    Map countParentScore(@Param("id") String id);

    /**
     *  通过工单号，公司ID，LibaryID，更新分数
     * @param libraryId
     * @param score
     * @param orderNum
     * @param companyId
     */
    @UpdateProvider(type = MarketQuestionOrderSQL.class, method = "updateOrderScoreByOrderNumSQL")
    void updateOrderScoreByOrderNum(@Param("libraryId") String libraryId, @Param("score") int score, @Param("orderNum") String orderNum, @Param("companyId") String companyId);
}
